package top.zywork.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.common.StringUtils;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.ActivityDTO;
import top.zywork.query.ActivityQuery;
import top.zywork.security.SecurityUtils;
import top.zywork.service.ActivityService;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.PagerVO;
import top.zywork.vo.ActivityVO;

import java.util.List;

/**
 * ActivityController控制器类<br/>
 *
 * 创建于2020-05-13<br/>
 *
 * @author http://zywork.top 危锦辉
 * @version 1.0
 */
@RestController
@RequestMapping("/activity")
@Slf4j
public class ActivityController extends BaseController {

    private ActivityService activityService;

    @PostMapping("admin/save")
    @SysLog(description = "后台保存ActivityVO")
    public ResponseStatusVO save(@RequestBody @Validated ActivityVO activityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        activityService.saveData(BeanUtils.copy(activityVO, ActivityDTO.class));
        return ResponseStatusVO.ok("添加成功", null);
    }

    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存ActivityVO")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<ActivityVO> activityVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        activityService.saveBatch(BeanUtils.copyListObj(activityVOList, ActivityDTO.class));
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/remove/{id}")
    @SysLog(description = "后台删除ActivityVO")
    public ResponseStatusVO removeById(@PathVariable("id") Long id) {
        activityService.removeById(id);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @PostMapping("admin/batch-remove")
    @SysLog(description = "后台批量删除ActivityVO")
    public ResponseStatusVO removeByIds(@RequestBody String[] ids) {
        activityService.removeByIds(StringUtils.strArrayToLongArray(ids));
        return ResponseStatusVO.ok("批量删除成功", null);
    }

    @PostMapping("admin/update")
    @SysLog(description = "后台更新ActivityVO")
    public ResponseStatusVO update(@RequestBody @Validated ActivityVO activityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        int updateRows = activityService.update(BeanUtils.copy(activityVO, ActivityDTO.class));
        if (updateRows == 1) {
            return ResponseStatusVO.ok("更新成功", null);
        } else {
            return ResponseStatusVO.dataError("数据版本号有问题，在此更新前数据已被更新", null);
        }
    }

    @PostMapping("admin/batch-update")
    @SysLog(description = "后台批量更新ActivityVO")
    public ResponseStatusVO updateBatch(@RequestBody @Validated List<ActivityVO> activityVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        activityService.updateBatch(BeanUtils.copyListObj(activityVOList, ActivityDTO.class));
        return ResponseStatusVO.ok("批量更新成功", null);
    }

    @PostMapping("admin/active")
    @SysLog(description = "后台激活或冻结ActivityVO")
    public ResponseStatusVO active(@RequestBody ActivityVO activityVO) {
        activityService.update(BeanUtils.copy(activityVO, ActivityDTO.class));
        return ResponseStatusVO.ok("激活或冻结成功", null);
    }

    @PostMapping("admin/batch-active")
    @SysLog(description = "后台批量激活或冻结ActivityVO")
    public ResponseStatusVO activeBatch(@RequestBody @Validated List<ActivityVO> activityVOList) {
        activityService.updateBatch(BeanUtils.copyListObj(activityVOList, ActivityDTO.class));
        return ResponseStatusVO.ok("批量激活或冻结成功", null);
    }

    @GetMapping("admin/one/{id}")
    public ResponseStatusVO getById(@PathVariable("id") Long id) {
        ActivityVO activityVO = new ActivityVO();
        Object obj = activityService.getById(id);
        if (obj != null) {
            activityVO = BeanUtils.copy(obj, ActivityVO.class);
        }
        return ResponseStatusVO.ok("查询成功", activityVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = activityService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), ActivityVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody ActivityQuery activityQuery) {
        PagerDTO pagerDTO = activityService.listAllByCondition(activityQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), ActivityVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody ActivityQuery activityQuery) {
        PagerDTO pagerDTO = activityService.listPageByCondition(activityQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), ActivityVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("any/all-cond")
    public ResponseStatusVO anyListAllCond(@RequestBody ActivityQuery activityQuery) {
        return listAllByCondition(activityQuery);
    }

    @GetMapping("any/all")
    public ResponseStatusVO anyListAll() {
        return listAll();
    }

    @PostMapping("user/save")
    public ResponseStatusVO userSave(@RequestBody @Validated ActivityVO activityVO, BindingResult bindingResult) {
        activityVO.setUserId(SecurityUtils.getJwtUser().getUserId());
        return save(activityVO, bindingResult);
    }

    /**
     * 获取当前用户参与的所有活动信息
     * @return
     */
    @GetMapping("user/my-activity")
    public ResponseStatusVO getUserAllActivity() {
        List<ActivityVO> activityVOList = activityService.getAllUserActivity(SecurityUtils.getJwtUser().getUserId());
        return ResponseStatusVO.ok("查询成功", activityVOList);
    }

    @GetMapping("any/one/{id}")
    public ResponseStatusVO anyGetById(@PathVariable("id") Long id) {
        return getById(id);
    }

    @Autowired
    public void setActivityService(ActivityService activityService) {
        this.activityService = activityService;
    }
}